// 全局请求封装
import baseApis from '@/config/index.config.js';
const baseApi = baseApis.baseUrl
// const baseApi = 'https://api.jht.cash'
// const baseApi = 'https://api.yj.shsdxr.com'
// 需要修改token，和根据实际修改请求头
export default (params) => {
	let url = params.url;
	let method = params.method || "get";
	let data = params.data || {};
	let header = {}
	console.log(uni.getStorageSync("channelid"),'获取到cid了吗')
	header['channeid'] = uni.getStorageSync("channelid");
	header['platform'] = "H5-" + uni.getSystemInfoSync().platform
	header['appid'] = '1'

	// 获取本地token
	if (uni.getStorageSync("token")) {
		header['token'] = uni.getStorageSync("token");
	}

	return new Promise((resolve, reject) => {
		uni.request({
			url: baseApi + url,
			method: method,
			header: header,
			data: data,
			success(response) {
				const res = response
				// 根据返回的状态码做出对应的操作
				//获取成功
				console.log(res);
				if (res.statusCode == 200) {
					if (res.data.code != 100000) {
						uni.showToast({
							title: res.data.msg || '请求失败',
							icon: 'none',
							duration: 2000
						})
						// setTimeout(() => {
							
						// 	let pages = getCurrentPages();
						// 	let currentPage = pages[pages.length - 1];
						// 	if(currentPage.route!='pages/index/index'){
						// 		uni.navigateTo({
						// 			url: '/pages/list/index'
						// 		})
						// 	}
						
						// }, 2000);

					} else {
						resolve(res.data);
					}

				} else {
					uni.clearStorageSync()
					switch (res.statusCode) {
						case 401:
							uni.showModal({
								title: "提示",
								content: "请登录",
								showCancel: false,
								success(res) {
									setTimeout(() => {
										uni.navigateTo({
											url: "/pages/login/index",
										})
									}, 1000);
								},
							});
							break;
						case 404:
							uni.showToast({
								title: '请求地址不存在...',
								duration: 2000,
								icon:'none'
							})
							break;
						default:
							uni.showToast({
								title: '请重试...',
								duration: 2000,
								icon:'none'
							})
							break;
					}
				}
			},
			fail(err) {
				console.log(err)
				if (err.errMsg.indexOf('request:fail') !== -1) {
					wx.showToast({
						title: '网络异常',
						icon: "error",
						duration: 2000
					})
				} else {
					wx.showToast({
						title: '未知异常',
						duration: 2000
					})
				}
				reject(err);

			},
			// complete() {
			// 	// 不管成功还是失败都会执行
			// 	uni.hideLoading();
			// 	uni.hideToast();
			// }
		});
	}).catch((e) => {});
};